function [x,output]=maxall_shareimmediate(xguess,index,ew,waut,bind,nobind,gammasub);
global gammagrid table grid states ewdev S Sdev agent statedev P wdev
global beta Y rho 
objval = [];
output=[];
lb=[];
ub=[];
options = optimoptions(@fmincon,'Display','iter','Algorithm','active-set','ConstraintTolerance',1e-3,'MaxFunctionEvaluations',15000,'OptimalityTolerance',1e-4);
[x,fval,exitflag]=fmincon(@objfun,xguess,[],[],[],[],lb,ub,@constrfun,options);
output(6)=exitflag;
function f = objfun(x);
i=index(1);
j=index(2);
d=index(3);
k=index(4);         %%%coalition of constrained agents
h=index(5);         %%%no of constrained agents
consu(1:agent-1) = x(1:agent-1);
consu(agent)=Y(j)-sum(x(1:agent-1));
method=1;

if method==1
wint=interpolation_4agents(ew, index,x./Y(j));
else
wint=chebyshevinterpolation(ew,index, x/Y(j),coeff,agent);
end  
for g=1:agent
output(g)=utility(rho,consu(g))+beta*wint(g);
end

objval = gammagrid(i,1)*output(1) + gammagrid(i,2)*output(2) + gammagrid(i,3)*output(3) + gammagrid(i,4)*output(4);
f=-objval;
end
function [cineq,ceq]=constrfun(x);
i=index(1);
j=index(2);
d=index(3);
k=index(4);         %%%coalition of constrained agents
h=index(5);         %%%no of constrained agents
consu(1:agent-1) = x(1:agent-1);
consu(agent)=Y(j)-sum(x(1:agent-1));
method=1;

if method==1
wint=interpolation_4agents(ew, index,x./Y(j));
else
wint=chebyshevinterpolation(ew,index, x/Y(j),coeff,agent);
end  
for g=1:agent
output(g)=utility(rho,consu(g))+beta*wint(g);
end


for r=1:length(bind)
if h>1
ceq(agent+r) = wdev(d,statedev(j,k,h),r,h) - output(bind(r));
else
ceq(agent+1)=(utility(rho,S(j,bind(r)))+beta*P(j,:)*waut(:,bind(r))-output(bind(r)));
end
end
cineq=[];
end
end